'use strict';
 const db = uniCloud.database();

exports.main = async (event, context) => {
  //event为客户端上传的参数
  // console.log('event : ' + event)
  const collection = db.collection('comments');
  const c2=db.collection('posts');
  let comment={	
	"user_id":event.user_id,
	"user_name":event.user_name,
	"icon":event.icon,
	"content":event.content,
	"time":event.time,
	"p_id":event.p_id
  }
  let res=await collection.add(comment);
  res=await c2.where({"post_id":event.p_id}).get();
  let count=res.data[0].comment_count;
  count=parseInt(count)+1;
  let _id=res.data[0]._id;
  console.log(count);
  res=await c2.doc(_id).update({
	  comment_count:   count.toString()
});
  //返回数据给客户端
  return {status:2}
};
